<?php
/**
 * 分类modle
 * User: Administrator
 * Date: 14-5-6
 * Time: 下午6:59
 */
class Mcolumn extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    function checkColumnByName($columnName)
    {
        $sql = "select *
				from `tbcolumn`
				where `name` = ? limit 1";

        $query = $this->db->query($sql,array("$columnName"));
        $return = array();
        foreach($query->result() as $value){
            if(!empty($value->name)){
                $return['name'] = $value->name;
                $return['id'] = $value->id;
            }
        }
        return  $return;
    }

    function addColumn($columnName)
    {
        $sql = "INSERT INTO `tbcolumn`(`name`,`create_time`)VALUES(?,now())";

        $this->db->query($sql,array("$columnName"));
        return  $this->db->affected_rows();
    }

    function editColumn($id,$columnName)
    {
        $sql = "UPDATE `tbcolumn` set `name` = ? WHERE `id` = ?";

        $this->db->query($sql,array("$columnName",$id));
    }

    function loadColumn()
    {
        $sql = "select * from `tbcolumn`";

        $query = $this->db->query($sql);
        return $query->result();
    }


    function getColumnById($id)
    {
        $sql = "select *
				from `tbcolumn`
				where `id` = ? ";

        $query = $this->db->query($sql,array($id));
        $return = array();
        foreach($query->result() as $value){
            if(!empty($value->name)){
                $return['name'] = $value->name;
                $return['id'] = $value->id;
            }
        }
        return  $return;
    }

    function deleteColumn($ids)
    {
        $sql = "DELETE FROM `tbcolumn` where `id` in ($ids)";

        $this->db->query($sql);
    }

}